﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LLFM.CAT.Model;
using Common;
using LLFM.CAT.Service;
using System.Text.RegularExpressions;

public partial class Admin_Ajax_EditSeriesAjax : AdminPage
{
    String State = String.Empty;
    List<M_SeriesImages> mImgs = null;
    string strImagePath = RequestHelper.GetString("photoArray");
    string name = RequestHelper.GetString("Name");
    string workWeight = RequestHelper.GetString("WorkWeight");
    string shortDescription = RequestHelper.GetString("ShortDescription");
    string advantage = RequestHelper.GetString("Advantage");
    string description = RequestHelper.GetString("Description");
    int seriesId = RequestHelper.GetInt("seriesId", 0);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Common.RequestHelper.IsAjaxRequest)
        {
            string p = @"/Date\((\d+)\)/";
            MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
            Regex reg = new Regex(p);
            strImagePath = reg.Replace(strImagePath, matchEvaluator);

            mImgs = JsonHelper.Deserialize<List<M_SeriesImages>>(strImagePath);
            State = "SUCCESS";
            bool verifyResult = verify();
            if (!verifyResult)
            {
                Response.Write(State);
                Response.End();
            }

            ProductSubmit();
            Response.Write("true");
            Response.End();
        }
    }

    private static string ConvertJsonDateToDateString(Match m)
    {
        string result = string.Empty;
        result = DateTime.Now.ToString();
        return result;
    }

    private void ProductSubmit()
    {
        List<M_SeriesImages> oldSeriesImgs = SeriesImagesService.GetSeriesImagesList(seriesId, 1);
        List<M_SeriesImages> notDelImgs = new List<M_SeriesImages>();
        string coverImage = "";
        int coverImgId = 0;
        if (mImgs.Count > 0)
        {
            coverImage = mImgs[0].ImageUrl;
            coverImgId = mImgs[0].Id;
        }
        SeriesService.UpdateSeries(seriesId, name, workWeight
            , advantage, shortDescription, description, "SmallCoverImage"
            , coverImage, coverImgId, 1, DateTime.Now);
        for (int i = 0; i < mImgs.Count; i++)
        {
            if (mImgs[i].Id == 0)
            {
                SeriesImagesService.CreateSeriesImages(mImgs[i].ImageUrl, mImgs[i].Name, seriesId, i + 1, (short)1);
            }
        }
        List<M_SeriesImages> delImgs = oldSeriesImgs.FindAll(o => !notDelImgs.Exists(g => g.Id == o.Id));
        SeriesImagesService.DelImgs(delImgs);

    }

    public bool verify()
    {
        bool verifyValue = true;
        if (string.IsNullOrEmpty(name.Trim()) || name.Length > 26)
        {
            State = "系列名称为空或超过指定长度26";
            verifyValue = false;
        }
        else if (string.IsNullOrEmpty(workWeight) || workWeight.Length > 20)
        {
            State = string.Format("工作重量为空或超长");
            verifyValue = false;
        }
        else if (string.IsNullOrEmpty(shortDescription) || shortDescription.Length > 1000)
        {
            State = string.Format("简短描述为空或超长");
            verifyValue = false;
        }
        else if (mImgs.Count > 10)
        {
            State = "上传图片数量超过5张";
            verifyValue = false;
        }
        return verifyValue;
    }



}